<?php

namespace App\Repository\Anchor;

use App\Models\Anchor\AnchorTypeModel as model;
use App\Repository\BaseRepository;

class AnchorTypeRepository extends BaseRepository
{
    public $model;
    public $log;

    public function __construct()
    {
        $this->model = new model();
        parent::__construct();
    }

    //列表
    public function index($where)
    {
        $cron = $where;
        $cron['order'] = ['orderby' => 'id', 'orderdir' => 'desc'];
        $dataArr = $this->search($this->model, $cron);
//      if (!empty($data['data'])) {
//          $data['data'] = usfirstArr($data['data']);
//      }
        if (is_array($dataArr)) {
            $dataArr['statusList'] = config('card.status');
        }
        return $dataArr;
    }

    public function add($data)
    {
        //必填 字段
        $mustField = [
            'name'
        ];
        $optionalFields = $this->model->getTableField();
        $msgField = [
            'name' => '类型名字不能为空'
        ];
        $insertData = $this->getRealData($data, $mustField, $optionalFields, $msgField);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }
        $isTrue = $this->model->where(['name' => $insertData['name']])->first();
        if ($isTrue) {
            return $this->returnMsg(1, '类型已经存在');
        }
//        $insertData['created_at'] = date('Y-m-d H:i:s');
        // $insertData['status'] = 1;
        // 查询是否存在 如果存在 直接流转
        $res = $this->model->insert($insertData);
        if ($res) {
            return $this->returnMsg(200, '添加成功');
        }
        return $this->returnMsg(1, '添加失败');
    }

    //删除
    public function del($id)
    {
        $where = ["id" => $id];
        $result = $this->model->where($where)->count();
        if ($result) {
            $del = $this->model->where($where)->delete();
            return $this->returnMsg(200, "删除成功");
        }
        return $this->returnMsg(1, "数据不存在");
    }

    //修改
    public function edit($request)
    {
        if (empty($request)) {
            return $this->returnMsg(2, '修改参数不能为空');
        }
        $optionalFields = $this->model->getTableField();
        $mustField = [
            'name'
        ];
        $msgField = [
            'name' => '类型名字不能为空'
        ];
        $updateData = $this->getRealData($request, $mustField, $optionalFields, $msgField);
        if (!empty($updateData) && is_string($updateData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $updateData
            );
        }
        $id = $request->get('id');
        $isTrue = $this->model->where([['name', '=', $updateData['name']], ['id', '<>', $id]])->first();
        if ($isTrue) {
            return $this->returnMsg(1, '类型已经存在');
        }
        $where = [['id', '=', $id]];
        $haveData = $this->model->where($where)->count("id");
        if (!$haveData) {
            return $this->returnMsg(100, '未查询到修改的数据');
        }
//        $anchor->name="测试333";
//        $res=$anchor->save();
        $res = $this->model->where($where)->update($updateData);
        if (false !== $res) {
            return $this->returnMsg(200, '修改成功!');
        }
        return $this->returnMsg(3, '请重新修改');
    }

    public function updateStatus($id, $status)
    {
        $updateData = ["status" => $status];
        $res = $this->model->where(["id" => $id])->update($updateData);
        if (false !== $res) {
            return $this->returnMsg(200, '修改成功');
        }
        return $this->returnMsg(3, '请重新修改');
    }

}
